<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionView::Helpers::FormOptionsHelper</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionView::Helpers::FormOptionsHelper</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_view/helpers/form_options_helper_rb.html">
                vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Provides a number of methods for turning different kinds of containers into
a set of option tags.
</p>
<h2>Options</h2>
<p>
The <tt><a
href="FormOptionsHelper.html#M002149">collection_select</a></tt>,
<tt>country_select</tt>, <tt><a
href="FormOptionsHelper.html#M002148">select</a></tt>, and <tt><a
href="FormOptionsHelper.html#M002150">time_zone_select</a></tt> methods
take an <tt>options</tt> parameter, a hash.
</p>
<ul>
<li><tt>:include_blank</tt> - set to true or a prompt string if the first
option element of the <a href="FormOptionsHelper.html#M002148">select</a>
element is a blank. Useful if there is not a default value required for the
<a href="FormOptionsHelper.html#M002148">select</a> element.

</li>
</ul>
<p>
For example,
</p>
<pre>
  select(&quot;post&quot;, &quot;category&quot;, Post::CATEGORIES, {:include_blank =&gt; true})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[category]&quot;&gt;
    &lt;option&gt;&lt;/option&gt;
    &lt;option&gt;joke&lt;/option&gt;
    &lt;option&gt;poem&lt;/option&gt;
  &lt;/select&gt;
</pre>
<p>
Another common case is a <a
href="FormOptionsHelper.html#M002148">select</a> tag for an
<tt>belongs_to</tt>-associated object.
</p>
<p>
Example with @post.person_id =&gt; 2:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank =&gt; 'None'})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;None&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot; selected=&quot;selected&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>
<ul>
<li><tt>:prompt</tt> - set to true or a prompt string. When the <a
href="FormOptionsHelper.html#M002148">select</a> element doesn&#8216;t have
a value yet, this prepends an option with a generic prompt &#8212;
&quot;Please <a href="FormOptionsHelper.html#M002148">select</a>&quot;
&#8212; or the given prompt string.

</li>
</ul>
<p>
Example:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt =&gt; 'Select Person'})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Select Person&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>
<p>
Like the other form helpers, <tt><a
href="FormOptionsHelper.html#M002148">select</a></tt> can accept an
<tt>:index</tt> option to manually set the ID used in the resulting output.
Unlike other helpers, <tt><a
href="FormOptionsHelper.html#M002148">select</a></tt> expects this option
to be in the <tt>html_options</tt> parameter.
</p>
<p>
Example:
</p>
<pre>
  select(&quot;album[]&quot;, &quot;genre&quot;, %w[rap rock country], {}, { :index =&gt; nil })
</pre>
<p>
becomes:
</p>
<pre>
  &lt;select name=&quot;album[][genre]&quot; id=&quot;album__genre&quot;&gt;
    &lt;option value=&quot;rap&quot;&gt;rap&lt;/option&gt;
    &lt;option value=&quot;rock&quot;&gt;rock&lt;/option&gt;
    &lt;option value=&quot;country&quot;&gt;country&lt;/option&gt;
  &lt;/select&gt;
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M002149">collection_select</a>&nbsp;&nbsp;
      <a href="#M002153">option_groups_from_collection_for_select</a>&nbsp;&nbsp;
      <a href="#M002151">options_for_select</a>&nbsp;&nbsp;
      <a href="#M002152">options_from_collection_for_select</a>&nbsp;&nbsp;
      <a href="#M002148">select</a>&nbsp;&nbsp;
      <a href="#M002154">time_zone_options_for_select</a>&nbsp;&nbsp;
      <a href="#M002150">time_zone_select</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../../ERB/Util.html">ERB::Util</a></span>
      </div>
    </div>

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M002149" class="method-detail">
        <a name="M002149"></a>

        <div class="method-heading">
          <a href="#M002149" class="method-signature">
          <span class="method-name">collection_select</span><span class="method-args">(object, method, collection, value_method, text_method, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns <tt>&lt;<a
href="FormOptionsHelper.html#M002148">select</a>&gt;</tt> and
<tt>&lt;option&gt;</tt> tags for the collection of existing return values
of <tt>method</tt> for <tt>object</tt>&#8216;s class. The value returned
from calling <tt>method</tt> on the instance <tt>object</tt> will be
selected. If calling <tt>method</tt> returns <tt>nil</tt>, no selection is
made without including <tt>:prompt</tt> or <tt>:include_blank</tt> in the
<tt>options</tt> hash.
</p>
<p>
The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods
to be called on each member of <tt>collection</tt>. The return values are
used as the <tt>value</tt> attribute and contents of each
<tt>&lt;option&gt;</tt> tag, respectively.
</p>
<p>
Example object structure for use with this method:
</p>
<pre>
  class Post &lt; ActiveRecord::Base
    belongs_to :author
  end
  class Author &lt; ActiveRecord::Base
    has_many :posts
    def name_with_initial
      &quot;#{first_name.first}. #{last_name}&quot;
    end
  end
</pre>
<p>
Sample usage (selecting the associated Author for an instance of Post,
<tt>@post</tt>):
</p>
<pre>
  collection_select(:post, :author_id, Author.find(:all), :id, :name_with_initial, {:prompt =&gt; true})
</pre>
<p>
If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
</p>
<pre>
  &lt;select name=&quot;post[author_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;D. Heinemeier Hansson&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;D. Thomas&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;M. Clark&lt;/option&gt;
  &lt;/select&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002149-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002149-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 132</span>
132:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">collection_select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
133:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_collection_select_tag</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
134:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002153" class="method-detail">
        <a name="M002153"></a>

        <div class="method-heading">
          <a href="#M002153" class="method-signature">
          <span class="method-name">option_groups_from_collection_for_select</span><span class="method-args">(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of <tt>&lt;option&gt;</tt> tags, like <tt><a
href="FormOptionsHelper.html#M002152">options_from_collection_for_select</a></tt>,
but groups them by <tt>&lt;optgroup&gt;</tt> tags based on the object
relationships of the arguments.
</p>
<p>
Parameters:
</p>
<ul>
<li><tt>collection</tt> - An array of objects representing the
<tt>&lt;optgroup&gt;</tt> tags.

</li>
<li><tt>group_method</tt> - The name of a method which, when called on a member
of <tt>collection</tt>, returns an array of child objects representing the
<tt>&lt;option&gt;</tt> tags.

</li>
<li>group_label_method+ - The name of a method which, when called on a member
of <tt>collection</tt>, returns a string to be used as the <tt>label</tt>
attribute for its <tt>&lt;optgroup&gt;</tt> tag.

</li>
<li><tt>option_key_method</tt> - The name of a method which, when called on a
child object of a member of <tt>collection</tt>, returns a value to be used
as the <tt>value</tt> attribute for its <tt>&lt;option&gt;</tt> tag.

</li>
<li><tt>option_value_method</tt> - The name of a method which, when called on a
child object of a member of <tt>collection</tt>, returns a value to be used
as the contents of its <tt>&lt;option&gt;</tt> tag.

</li>
<li><tt>selected_key</tt> - A value equal to the <tt>value</tt> attribute for
one of the <tt>&lt;option&gt;</tt> tags, which will have the
<tt>selected</tt> attribute set. Corresponds to the return value of one of
the calls to <tt>option_key_method</tt>. If <tt>nil</tt>, no selection is
made.

</li>
</ul>
<p>
Example object structure for use with this method:
</p>
<pre>
  class Continent &lt; ActiveRecord::Base
    has_many :countries
    # attribs: id, name
  end
  class Country &lt; ActiveRecord::Base
    belongs_to :continent
    # attribs: id, name, continent_id
  end
</pre>
<p>
Sample usage:
</p>
<pre>
  option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
</pre>
<p>
Possible output:
</p>
<pre>
  &lt;optgroup label=&quot;Africa&quot;&gt;
    &lt;option value=&quot;1&quot;&gt;Egypt&lt;/option&gt;
    &lt;option value=&quot;4&quot;&gt;Rwanda&lt;/option&gt;
    ...
  &lt;/optgroup&gt;
  &lt;optgroup label=&quot;Asia&quot;&gt;
    &lt;option value=&quot;3&quot; selected=&quot;selected&quot;&gt;China&lt;/option&gt;
    &lt;option value=&quot;12&quot;&gt;India&lt;/option&gt;
    &lt;option value=&quot;5&quot;&gt;Japan&lt;/option&gt;
    ...
  &lt;/optgroup&gt;
</pre>
<p>
<b>Note:</b> Only the <tt>&lt;optgroup&gt;</tt> and <tt>&lt;option&gt;</tt>
tags are returned, so you still have to wrap the output in an appropriate
<tt>&lt;<a href="FormOptionsHelper.html#M002148">select</a>&gt;</tt> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002153-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002153-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 263</span>
263:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">option_groups_from_collection_for_select</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">group_method</span>, <span class="ruby-identifier">group_label_method</span>, <span class="ruby-identifier">option_key_method</span>, <span class="ruby-identifier">option_value_method</span>, <span class="ruby-identifier">selected_key</span> = <span class="ruby-keyword kw">nil</span>)
264:         <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value str">&quot;&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">options_for_select</span>, <span class="ruby-identifier">group</span><span class="ruby-operator">|</span>
265:           <span class="ruby-identifier">group_label_string</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;group.#{group_label_method}&quot;</span>)
266:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;&lt;optgroup label=\&quot;#{html_escape(group_label_string)}\&quot;&gt;&quot;</span>
267:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_from_collection_for_select</span>(<span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;group.#{group_method}&quot;</span>), <span class="ruby-identifier">option_key_method</span>, <span class="ruby-identifier">option_value_method</span>, <span class="ruby-identifier">selected_key</span>)
268:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">'&lt;/optgroup&gt;'</span>
269:         <span class="ruby-keyword kw">end</span>
270:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002151" class="method-detail">
        <a name="M002151"></a>

        <div class="method-heading">
          <a href="#M002151" class="method-signature">
          <span class="method-name">options_for_select</span><span class="method-args">(container, selected = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Accepts a container (hash, array, enumerable, your type) and returns a
string of option tags. Given a container where the elements respond to
first and last (such as a two-element array), the &quot;lasts&quot; serve
as option values and the &quot;firsts&quot; as option text. Hashes are
turned into this form automatically, so the keys become &quot;firsts&quot;
and values become lasts. If <tt>selected</tt> is specified, the matching
&quot;last&quot; or element will get the selected option-tag.
<tt>selected</tt> may also be an array of values to be selected when using
a multiple <a href="FormOptionsHelper.html#M002148">select</a>.
</p>
<p>
Examples (call, result):
</p>
<pre>
  options_for_select([[&quot;Dollar&quot;, &quot;$&quot;], [&quot;Kroner&quot;, &quot;DKK&quot;]])
    &lt;option value=&quot;$&quot;&gt;Dollar&lt;/option&gt;\n&lt;option value=&quot;DKK&quot;&gt;Kroner&lt;/option&gt;

  options_for_select([ &quot;VISA&quot;, &quot;MasterCard&quot; ], &quot;MasterCard&quot;)
    &lt;option&gt;VISA&lt;/option&gt;\n&lt;option selected=&quot;selected&quot;&gt;MasterCard&lt;/option&gt;

  options_for_select({ &quot;Basic&quot; =&gt; &quot;$20&quot;, &quot;Plus&quot; =&gt; &quot;$40&quot; }, &quot;$40&quot;)
    &lt;option value=&quot;$20&quot;&gt;Basic&lt;/option&gt;\n&lt;option value=&quot;$40&quot; selected=&quot;selected&quot;&gt;Plus&lt;/option&gt;

  options_for_select([ &quot;VISA&quot;, &quot;MasterCard&quot;, &quot;Discover&quot; ], [&quot;VISA&quot;, &quot;Discover&quot;])
    &lt;option selected=&quot;selected&quot;&gt;VISA&lt;/option&gt;\n&lt;option&gt;MasterCard&lt;/option&gt;\n&lt;option selected=&quot;selected&quot;&gt;Discover&lt;/option&gt;
</pre>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M002148">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002151-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002151-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 190</span>
190:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">container</span>, <span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>)
191:         <span class="ruby-identifier">container</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-keyword kw">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">container</span>
192: 
193:         <span class="ruby-identifier">options_for_select</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">inject</span>([]) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">options</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
194:           <span class="ruby-identifier">text</span>, <span class="ruby-identifier">value</span> = <span class="ruby-identifier">option_text_and_value</span>(<span class="ruby-identifier">element</span>)
195:           <span class="ruby-identifier">selected_attribute</span> = <span class="ruby-value str">' selected=&quot;selected&quot;'</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">option_value_selected?</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">selected</span>)
196:           <span class="ruby-identifier">options</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">%(&lt;option value=&quot;#{html_escape(value.to_s)}&quot;#{selected_attribute}&gt;#{html_escape(text.to_s)}&lt;/option&gt;)</span>
197:         <span class="ruby-keyword kw">end</span>
198: 
199:         <span class="ruby-identifier">options_for_select</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
200:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002152" class="method-detail">
        <a name="M002152"></a>

        <div class="method-heading">
          <a href="#M002152" class="method-signature">
          <span class="method-name">options_from_collection_for_select</span><span class="method-args">(collection, value_method, text_method, selected = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of option tags that have been compiled by iterating over
the <tt>collection</tt> and assigning the the result of a call to the
<tt>value_method</tt> as the option value and the <tt>text_method</tt> as
the option text. If <tt>selected</tt> is specified, the element returning a
match on <tt>value_method</tt> will get the selected option tag.
</p>
<p>
Example (call, result). Imagine a loop iterating over each <tt>person</tt>
in <tt>@project.people</tt> to generate an input tag:
</p>
<pre>
  options_from_collection_for_select(@project.people, &quot;id&quot;, &quot;name&quot;)
    &lt;option value=&quot;#{person.id}&quot;&gt;#{person.name}&lt;/option&gt;
</pre>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M002148">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002152-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002152-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 211</span>
211:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">options_from_collection_for_select</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>)
212:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
213:           [<span class="ruby-identifier">element</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">text_method</span>), <span class="ruby-identifier">element</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">value_method</span>)]
214:         <span class="ruby-keyword kw">end</span>
215:         <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">selected</span>)
216:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002148" class="method-detail">
        <a name="M002148"></a>

        <div class="method-heading">
          <a href="#M002148" class="method-signature">
          <span class="method-name">select</span><span class="method-args">(object, method, choices, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Create a <a href="FormOptionsHelper.html#M002148">select</a> tag and a
series of contained option tags for the provided object and method. The
option currently held by the object will be selected, provided that the
object is available. See <a
href="FormOptionsHelper.html#M002151">options_for_select</a> for the
required format of the choices parameter.
</p>
<p>
Example with @post.person_id =&gt; 1:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, { :include_blank =&gt; true })
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;&lt;/option&gt;
    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>
<p>
This can be used to provide a default set of options in the standard way:
before rendering the create form, a new model instance is assigned the
default options and bound to @model_name. Usually this model is not saved
to the database. Instead, a second model object is created when the create
request is received. This allows the user to submit a form page more than
once with the expected results of creating multiple records. In addition,
this allows a single partial to be used to generate form inputs for both
edit and create forms.
</p>
<p>
By default, <tt>post.person_id</tt> is the selected option. Specify
<tt>:selected =&gt; value</tt> to use a different selection or
<tt>:selected =&gt; nil</tt> to leave all options unselected.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002148-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002148-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 98</span>
 98:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">choices</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
 99:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_select_tag</span>(<span class="ruby-identifier">choices</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
100:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002154" class="method-detail">
        <a name="M002154"></a>

        <div class="method-heading">
          <a href="#M002154" class="method-signature">
          <span class="method-name">time_zone_options_for_select</span><span class="method-args">(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of option tags for pretty much any time zone in the world.
Supply a TimeZone name as <tt>selected</tt> to have it marked as the
selected option tag. You can also supply an array of TimeZone objects as
<tt>priority_zones</tt>, so that they will be listed above the rest of the
(long) list. (You can use TimeZone.us_zones as a convenience for obtaining
a list of the US time zones, or a Regexp to <a
href="FormOptionsHelper.html#M002148">select</a> the zones of your choice)
</p>
<p>
The <tt>selected</tt> parameter must be either <tt>nil</tt>, or a string
that names a TimeZone.
</p>
<p>
By default, <tt>model</tt> is the TimeZone constant (which can be obtained
in Active Record as a value object). The only requirement is that the
<tt>model</tt> parameter be an object that responds to <tt>all</tt>, and
returns an array of objects that represent time zones.
</p>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M002148">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002154-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002154-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 290</span>
290:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">time_zone_options_for_select</span>(<span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">priority_zones</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">model</span> = <span class="ruby-operator">::</span><span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">TimeZone</span>)
291:         <span class="ruby-identifier">zone_options</span> = <span class="ruby-value str">&quot;&quot;</span>
292: 
293:         <span class="ruby-identifier">zones</span> = <span class="ruby-identifier">model</span>.<span class="ruby-identifier">all</span>
294:         <span class="ruby-identifier">convert_zones</span> = <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">list</span><span class="ruby-operator">|</span> <span class="ruby-identifier">list</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span> [ <span class="ruby-identifier">z</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-identifier">z</span>.<span class="ruby-identifier">name</span> ] } }
295: 
296:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">priority_zones</span>
297:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">priority_zones</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Regexp</span>)
298:             <span class="ruby-identifier">priority_zones</span> = <span class="ruby-identifier">model</span>.<span class="ruby-identifier">all</span>.<span class="ruby-identifier">find_all</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span> <span class="ruby-identifier">z</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">priority_zones</span>}
299:                 <span class="ruby-keyword kw">end</span>
300:           <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">convert_zones</span>[<span class="ruby-identifier">priority_zones</span>], <span class="ruby-identifier">selected</span>)
301:           <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;&lt;option value=\&quot;\&quot; disabled=\&quot;disabled\&quot;&gt;-------------&lt;/option&gt;\n&quot;</span>
302: 
303:           <span class="ruby-identifier">zones</span> = <span class="ruby-identifier">zones</span>.<span class="ruby-identifier">reject</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span> <span class="ruby-identifier">priority_zones</span>.<span class="ruby-identifier">include?</span>( <span class="ruby-identifier">z</span> ) }
304:         <span class="ruby-keyword kw">end</span>
305: 
306:         <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">convert_zones</span>[<span class="ruby-identifier">zones</span>], <span class="ruby-identifier">selected</span>)
307:         <span class="ruby-identifier">zone_options</span>
308:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002150" class="method-detail">
        <a name="M002150"></a>

        <div class="method-heading">
          <a href="#M002150" class="method-signature">
          <span class="method-name">time_zone_select</span><span class="method-args">(object, method, priority_zones = nil, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Return <a href="FormOptionsHelper.html#M002148">select</a> and option tags
for the given object and method, using <a
href="FormOptionsHelper.html#M002154">time_zone_options_for_select</a> to
generate the list of option tags.
</p>
<p>
In addition to the <tt>:include_blank</tt> option documented above, this
method also supports a <tt>:model</tt> option, which defaults to TimeZone.
This may be used by users to specify a different time zone model object.
(See <tt><a
href="FormOptionsHelper.html#M002154">time_zone_options_for_select</a></tt>
for more information.)
</p>
<p>
You can also supply an array of TimeZone objects as
<tt>priority_zones</tt>, so that they will be listed above the rest of the
(long) list. (You can use TimeZone.us_zones as a convenience for obtaining
a list of the US time zones, or a Regexp to <a
href="FormOptionsHelper.html#M002148">select</a> the zones of your choice)
</p>
<p>
Finally, this method supports a <tt>:default</tt> option, which selects a
default TimeZone if the object&#8216;s time zone is <tt>nil</tt>.
</p>
<p>
Examples:
</p>
<pre>
  time_zone_select( &quot;user&quot;, &quot;time_zone&quot;, nil, :include_blank =&gt; true)

  time_zone_select( &quot;user&quot;, &quot;time_zone&quot;, nil, :default =&gt; &quot;Pacific Time (US &amp; Canada)&quot; )

  time_zone_select( &quot;user&quot;, 'time_zone', TimeZone.us_zones, :default =&gt; &quot;Pacific Time (US &amp; Canada)&quot;)

  time_zone_select( &quot;user&quot;, 'time_zone', [ TimeZone['Alaska'], TimeZone['Hawaii'] ])

  time_zone_select( &quot;user&quot;, 'time_zone', /Australia/)

  time_zone_select( &quot;user&quot;, &quot;time_zone&quot;, TZInfo::Timezone.all.sort, :model =&gt; TZInfo::Timezone)
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002150-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002150-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 166</span>
166:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">time_zone_select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">priority_zones</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
167:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>,  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_time_zone_select_tag</span>(<span class="ruby-identifier">priority_zones</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
168:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>